import * as Ts from 'typescript';
import tsslint from '@tsslint/config';
import type { Rule, RuleContext } from '@tsslint/config';

function importX(): Rule {
  return tsslint.defineRule(
    ({
      typescript: ts,
      sourceFile,
      reportWarning,
      languageService,
    }: RuleContext) => {
      const Warning_Title = "Calls to 'import x' are not allowed.";
      const Fix_Title = 'Remove this import expression';

      ts.forEachChild(sourceFile, function cb(node: Ts.Node) {
        if (
          ts.isImportDeclaration(node) /* a.b.c */ &&
          ts.isStringLiteral(
            node.moduleSpecifier,
          ) /* node.expression: console */
        ) {
          reportWarning(
            Warning_Title + `'${node.moduleSpecifier.text}'`,
            node.moduleSpecifier.getStart(sourceFile) + 1,
            node.moduleSpecifier.getEnd() - 1,
          );
        }
      });
    },
  );
}
export default importX;
